package com.shopmall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shopmall.system.dao.UserInfoMapper;
import com.shopmall.system.dao.UserRoleMapper;
import com.shopmall.system.entity.UserInfo;
import com.shopmall.system.entity.UserRole;
import com.shopmall.system.service.IUserInfoService;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService  {
    @Resource
    UserInfoMapper userInfoMapper;
    @Resource
    BCryptPasswordEncoder passwordEncoder;
    @Resource
    UserRoleMapper userRoleMapper;
    @Override
    public IPage<UserInfo>  userList(UserInfo userInfo) {
        IPage<UserInfo> page = new Page<>(userInfo.getPageNum(),userInfo.getPageSize());
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(UserInfo::getId);
        IPage<UserInfo> userPage = this.baseMapper.selectPage(page, wrapper);
        List<UserInfo> records = userPage.getRecords();
        for (UserInfo record : records) {
            LambdaQueryWrapper<UserRole> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.eq(UserRole::getUid,record.getId());
            List<UserRole> userRoles = userRoleMapper.selectList(delWrapper);
            List<Integer> collect = userRoles.stream().map(UserRole::getRid).collect(Collectors.toList());
            record.setRoles(collect.toArray(new Integer[0]));
        }

        return userPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrUpdate(UserInfo userInfo) {
        int num=0;
        if(userInfo.getId()==null){
            String encode = passwordEncoder.encode(userInfo.getPassWord());
            userInfo.setPassword(encode);
            num = this.baseMapper.insert(userInfo);
            Integer[] roles = userInfo.getRoles();
            for (Integer role : roles) {
                UserRole userRole = new UserRole();
                userRole.setRid(role);
                userRole.setUid(Integer.valueOf(userInfo.getId()+""));
                userRoleMapper.insert(userRole);
            }
        }else{
            //删除用户角色关联
            LambdaQueryWrapper<UserRole> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.eq(UserRole::getUid,userInfo.getId());
            userRoleMapper.delete(delWrapper);
            Integer[] roles = userInfo.getRoles();
            for (Integer role : roles) {
                UserRole userRole = new UserRole();
                userRole.setRid(role);
                userRole.setUid(Integer.valueOf(userInfo.getId()+""));
                userRoleMapper.insert(userRole);
            }
            num = this.baseMapper.updateById(userInfo);
        }
        return num;
    }

    @Override
    public int deleteById(Integer id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public int updatePassword(UserInfo userInfo) {
        String encode = passwordEncoder.encode(userInfo.getNewPassWord());
        userInfo.setPassword(encode);
        int num=this.baseMapper.updateById(userInfo);
        return num;
    }


}
